{% extends "swarming/base.html" %}


{% block headers %}
<style>
  h1 {
    margin-top: 10px;
    margin-bottom: 10px;
  }

  table.layout_table {
    border-spacing: 0;
    font-family: monospace;
  }

  table.layout_table thead {
    background-color: #C0C0C0;
    font-weight:bold;
  }

  table.layout_table tbody tr:nth-child(even) {
    background-color: #eeeeee;
  }

  table.layout_table td:first-child {
    border-color: black;
    border-right: thin solid;
    padding-right: 1em;
  }

  table.layout_table td:nth-child(2) {
    padding-left: 1em;
  }

  table.layout_table td:nth-child(4) {
    border-color: black;
    border-right: thin solid;
    padding-right: 1em;
  }

  table.layout_table td:nth-child(5) {
    border-color: black;
    border-right: thin solid;
    padding-left: 1em;
    padding-right: 1em;
  }

  table.layout_table td:nth-child(6) {
    padding-left: 1em;
    padding-right: 0.5em;
  }

  table.alterning_background td:nth-child(3) {
    text-align: center;
    white-space: nowrap;
  }

  table.alterning_background td:nth-child(4) {
    text-align: right;
    white-space: nowrap;
  }

  table.alterning_background td:nth-child(5) {
    text-align: right;
    white-space: nowrap;
  }

  table.alterning_background td:nth-child(6) {
    white-space: nowrap;
  }

  table.alterning_background td:nth-child(7) {
    text-align: right;
    white-space: nowrap;
  }
</style>
<script>
  function radio_value(field) {
    var radios = document.getElementsByName(field);
    for (var i = 0, length = radios.length; i < length; i++) {
      if (radios[i].checked) {
        return radios[i].value;
      }
    }
  }

  function get_limit() {
    return document.getElementById('limitText').value;
  }

  function update_limit_text(val) {
    document.getElementById('limitText').value = val;
  }

  function update_limit_range(val) {
    document.getElementById('limitRange').value = val;
  }

  function update_sort() {
    // This resets the cursor and filters.
    var sort = radio_value("sort");
    var url = '/user/tasks?sort=' + sort + '&limit=' + get_limit();
    document.location.href = url;
  }

  function update_filter() {
    // This resets the cursor and sort.
    var state = radio_value("state");
    var url = '/user/tasks?state=' + state + '&limit=' + get_limit();
    document.location.href = url;
  }
</script>
{% endblock %}


{% block body %}
{% import 'swarming/bot_view.html' as bot_view %}

<h1>Tasks</h1>
<a href="/">Back to root</a>
<p>
<form id="filter" name="filter" method="GET">
<input type="range" id="limitRange" name="limit" value="{{limit}}" min=10
    max=500 step=5 onchange="update_limit_text(this.value);"
    onmousemove="update_limit_text(this.value);" >
<input type="text" id="limitText" value="{{limit}}" maxlength="3"
    onchange="update_limit_range(this.value);">
<input type="submit" value="Set limit">
<p>
<table class=layout_table title="Select order and filter">
  <thead>
    <tr>
      <td align=center>Sort</td>
      <td></td>
      <td>State (total last 24h)</td>
      <td></td>
      <td>Search by tag</td>
      <td>Search by task name</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        {% for key, name, item_title in sort_choices %}
          <label title="{{item_title}}">
            <input type="radio" name="sort" value="{{key}}"
                onchange="update_sort()"
                {% if sort == key %}checked{%endif%}>
              {{name}}
            </input>
          </label>
          <br>
        {% endfor %}
      </td>
      {% for state_column in state_choices %}
        <td>
          {% for key, name, item_title in state_column %}
            <label title="{{item_title}}">
              <input type="radio" name="state" value="{{key}}"
                  onchange="update_filter()"
                  {% if state == key %}checked{%endif%}>
                {{name}}
              </input>
            </label>
            <br>
          {% endfor %}
        </td>
        {% endfor %}
      <td>
        <div title="Task tags uses whole tag search ONLY. Using it resets sort to 'Created' and the task state filter to 'All'.">
          Task tags<br>
          (whole tag only, 1 per line)
          <br>
          <textarea name="task_tag" rows="3" cols="30">{{task_tag}}</textarea>
          <br>
          <input type="submit" value="Search">
        </div>
      </td>
      <td>
        <div title="Task name uses whole word search ONLY. Using it resets sort to 'Created' and the task state filter to 'All'.">
          Task name<br>
          (whole word only)
          <br>
          <input type="text" name="task_name" value="{{task_name}}">
          <br>
          <input type="submit" value="Search">
        </div>
      </td>
    </tr>
  <tbody>
</table>
</form>
<br>

{% macro total_pending_star() %}
  {% if has_pending %}*{% endif %}
  {% endmacro %}

{% macro total_running_star() %}
  {% if has_running %}*{% endif %}
{% endmacro %}

{% if tasks %}
  <table id="request-table" class="alterning_background"
      summary="This table lists all test requests">
    <thead>
      <th>Name</th>
      <th>Status</th>
      <th>Requested</th>
      <th>Pending</th>
      <th>Duration</th>
      <th>$USD</th>
      <th>User</th>
      <th>Bot</th>
      <th>Priority</th>
    </thead>
    <tbody>
      {% for task in tasks %}
        <tr class="request {% if task.failure or task.internal_failure %}failed_test{% endif%}">
          <td>
            <a href="/user/task/{{task.key_packed}}">{{task.name}}</a>
          </td>
          <td>
            {{task.to_string()|safe}}
            {% if task.can_be_canceled and is_admin %}
              <form id="cancel_{{task.key_packed}}" method="post"
                  action="/user/tasks/cancel">
                <input type="hidden" name="redirect_to" value="listing" />
                <input type="hidden" name="xsrf_token" value="{{xsrf_token}}" />
                <input type="hidden" name="task_id"
                    value="{{task.key_packed}}" />
                <input type="submit" value="Cancel" />
              </form>
            {% endif %}
          </td>
          <td>{{task.created_ts|succinctdatetimeformat}}</td>
          <td>
            {{bot_view.pending_star(task)}}{{task.pending_now(now)|timedeltaformat}}
          </td>
          <td>
            {{bot_view.running_star(task)}}{{task.duration_now(now)|timedeltaformat}}
          </td>
          <td>
            {% if task.cost_usd %}
              {{task.cost_usd|round(4) }} $
            {% elif task.cost_saved_usd %}
              <span style="color:green">-{{task.cost_saved_usd|round(4) }} $</span>
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td>
            {% if task.user %}
              <a href="/user/tasks?task_tag=user:{{task.user}}&limit={{limit}}">{{task.user}}</a>
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td>{{bot_view.bot_link(task.bot_id, is_privileged_user)}}</td>
          <td>{{task.priority}}</td>
        </tr>
      {% endfor %}
      {% if show_footer %}
        <tr>
          <td>&nbsp;</td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      {% endif %}
    </tbody>
    {% if show_footer %}
      <tfoot>
        <tr>
          <td></td>
          <td></td>
          <td><strong>Median</strong></td>
          <td>
            {{total_pending_star()}}{{pending_median|timedeltaformat}}
          </td>
          <td>
            {{total_running_star()}}{{duration_median|timedeltaformat}}
          </td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td><strong>Total: </strong>{{tasks|length}}</td>
          <td><strong>Average</strong></td>
          <td>
            {{total_pending_star()}}{{pending_average|timedeltaformat}}
          </td>
          <td>
            {{total_running_star()}}{{duration_average|timedeltaformat}}
          </td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td><strong>Sum</strong></td>
          <td>
            {{total_pending_star()}}{{pending_sum|timedeltaformat}}
          </td>
          <td>
            {{total_running_star()}}{{duration_sum|timedeltaformat}}
          </td>
          <td>
            {% if total_cost_usd %}
              {{total_cost_usd|round(4) }} $
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td><strong>Saved by deduping</strong></td>
          <td>
            ({{'%1.1f' % total_saved_percent}}%)
          </td>
          <td>
            {{total_saved|timedeltaformat}}
          </td>
          <td>
            {% if total_cost_saved_usd %}
              <span style="color:green">{{total_cost_saved_usd|round(4) }} $</span>
            {% else %}
              &#8209;&#8209;
            {% endif %}
          </td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tfoot>
    {% endif %}
  </table>
{% else %}
  No data to show for this selection.
{% endif %}

{% if cursor %}
<p>
{% if task_tag %}
  <a href="/user/tasks?task_tag={{task_tag}}&limit={{limit}}&cursor={{cursor}}">Next page</a>
{% elif task_name %}
  <a href="/user/tasks?task_name={{task_name}}&limit={{limit}}&cursor={{cursor}}">Next page</a>
{% else %}
  <a href="/user/tasks?sort={{sort}}&state={{state}}&limit={{limit}}&cursor={{cursor}}">Next page</a>
{% endif %}
{% endif %}

{% endblock %}
